
# import requests
# from bs4 import BeautifulSoup
 
# query = '现在几点钟了'


# search_url = f"https://www.baidu.com/s?wd={query}&rsv_spt=1&rsv_iqid=0xef21afb801187e61&issp=1&f=8&rsv_bp=1&rsv_idx=2&ie=utf-8&tn=57095150_10_oem_dg&rsv_enter=1&rsv_dl=tb&rsv_sug3=4&rsv_sug1=4&rsv_sug7=101&rsv_sug2=0&rsv_btype=i"
# headers = {
#     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
# }


# response = requests.get(search_url, headers=headers)
# response.encoding = response.apparent_encoding  # 确保正确处理编码
# print(response.text)
# soup = BeautifulSoup(response.text, 'html.parser')

#https://www.baidu.com/s?wd=%E7%8E%B0%E5%9C%A8%E5%87%A0%E7%82%B9%E9%92%9F%E4%BA%86&rsv_spt=1&rsv_iqid=0xef21afb801187e61&issp=1&f=8&rsv_bp=1&rsv_idx=2&ie=utf-8&tn=57095150_10_oem_dg&rsv_enter=1&rsv_dl=tb&rsv_sug3=4&rsv_sug1=4&rsv_sug7=101&rsv_sug2=0&rsv_btype=i&prefixsug=%25E7%258E%25B0%25E5%259C%25A8%25E5%2587%25A0%25E7%2582%25B9%25E9%2592%259F%25E4%25BA%2586&rsp=9&inputT=3768&rsv_sug4=5553&rsv_sug=1

from fastapi import FastAPI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_openai import ChatOpenAI
from langserve import add_routes

# 1. Create prompt template
system_template = "Translate the following into {language}:"
prompt_template = ChatPromptTemplate.from_messages([
    ('system', system_template),
    ('user', '{text}')
])

# 2. Create model
# model = ChatOpenAI()

api_key = "c27a61cf-658c-4b54-a4e9-d1a4574e6738"
api_base = "https://ark.cn-beijing.volces.com/api/v3"

model = ChatOpenAI(
    model_name="deepseek-r1-250528",
    openai_api_key=api_key,
    openai_api_base=api_base,
    temperature = 0
)



# 3. Create parser
parser = StrOutputParser()

# 4. Create chain
chain = prompt_template | model | parser


# 4. App definition
app = FastAPI(
  title="LangChain Server",
  version="1.0",
  description="A simple API server using LangChain's Runnable interfaces",
)

# 5. Adding chain route
add_routes(
    app,
    chain,
    path="/chain",
)

if __name__ == "__main__":
    import uvicorn

    uvicorn.run(app, host="localhost", port=8000)